import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:permission_handler/permission_handler.dart';

/// if (Platform.isIOS) {
//       return permission != Permission.unknown &&
//           permission != Permission.sms &&
//           permission != Permission.storage &&
//           permission != Permission.ignoreBatteryOptimizations &&
//           permission != Permission.accessMediaLocation &&
//           permission != Permission.activityRecognition &&
//           permission != Permission.manageExternalStorage &&
//           permission != Permission.systemAlertWindow &&
//           permission != Permission.requestInstallPackages &&
//           permission != Permission.accessNotificationPolicy &&
//           permission != Permission.bluetoothScan &&
//           permission != Permission.bluetoothAdvertise &&
//           permission != Permission.bluetoothConnect;
//     } else {
//       return permission != Permission.unknown &&
//           permission != Permission.mediaLibrary &&
//           permission != Permission.photos &&
//           permission != Permission.photosAddOnly &&
//           permission != Permission.reminders &&
//           permission != Permission.appTrackingTransparency &&
//           permission != Permission.criticalAlerts;
//     }

class PermissionHelper {
  static const String camera = "请开启使用相机权限";
  static const String camera_denied = "您已禁止并不再提示申请相机权限，请进入设置修改该权限！";
  static const String photos = "请开启相册访问权限";
  static const String photos_denied = "您已禁止并不再提示申请相册权限，请进入设置修改该权限！";
  static const String storage = "请开启使用储存权限";
  static const String storage_denied = "您已禁止并不再提示申请储存权限，请进入设置修改该权限！";
  static const String microphone = "请开启使用麦克风权限";
  static const String microphone_denied = "您已禁止并不再提示申请麦克风权限，请进入设置修改该权限！";
  static const String location = "请开启使用位置权限";
  static const String location_denied = "您已禁止并不再提示申请位置权限，请进入设置修改该权限！";
  static const String contacts = "请开启使用手机通讯录权限";
  static const String contacts_denied = "您已禁止并不再提示申请手机通讯录权限，请进入设置修改该权限！"; 

  // static VoidCallback defaultCall = () {};
  static Future<bool> checkCameraPermissions({Function? onSuccess}) async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    List<Permission> permissionList = [];
    if (Platform.isIOS) {
      permissionList = [Permission.camera,Permission.photosAddOnly];
    } else {
      if (androidInfo.version.sdkInt >= 32) {
        permissionList = [Permission.camera,Permission.photos, Permission.videos, Permission.audio];
      } else {
        permissionList = [Permission.camera,Permission.storage];
      }
    }
    List hints = [camera, storage];
    List hintSettings = [camera_denied, storage_denied];
    return await check(permissionList, onSuccess: onSuccess, onFailed: (permission) {
      EasyLoading.showError(hints[permissionList.indexOf(permission)]);
    }, onOpenSetting: (permission) {
      EasyLoading.showError(hintSettings[permissionList.indexOf(permission)]);
    });
  }

  static Future<bool> checkFilePermissions({Function? onSuccess}) async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    List<Permission> permissionList = [];
    if (Platform.isIOS) {
      permissionList = [Permission.photosAddOnly];
    } else {
      if (androidInfo.version.sdkInt >= 32) {
        permissionList = [Permission.photos, Permission.videos, Permission.audio];
      } else {
        permissionList = [Permission.storage];
      }
    }

    List hints = [storage];
    List hintSettings = [storage_denied];
    return await check(permissionList, onSuccess: onSuccess, onFailed: (permission) {
      EasyLoading.showError(hints[permissionList.indexOf(permission)]);
    }, onOpenSetting: (permission) {
      EasyLoading.showError(hintSettings[permissionList.indexOf(permission)]);
    });
  }

  static Future<bool> checkLocationPermissions({Function? onSuccess}) async {
    List<Permission> permissionList = [Permission.location];
    List hints = [location];
    List hintSettings = [location_denied];
    return await check(permissionList, onSuccess: onSuccess, onFailed: (permission) {
      EasyLoading.showError(hints[permissionList.indexOf(permission)]);
    }, onOpenSetting: (permission) {
      EasyLoading.showError(hintSettings[permissionList.indexOf(permission)]);
    });
  }

  ///检查权限
  static Future<bool> check(List<Permission> permissionList, {Function? onSuccess, Function? onFailed, Function? onOpenSetting}) async {
    bool flag = true;
    for (var value in permissionList) {
      var status = await value.status;
      if (!status.isGranted) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      Map<Permission, PermissionStatus> statuses = await permissionList.request();
      Permission? currentPermission;
      try {
        currentPermission = statuses.keys.firstWhere((element) => !(statuses[element]?.isGranted ?? false));
      } catch (e) {}
      if (currentPermission == null) {
        onSuccess?.call();
        return true;
      }
      if (statuses[currentPermission]!.isGranted) {
        onSuccess?.call();
        return true;
      } else if (statuses[currentPermission]!.isDenied) {
        onFailed?.call(currentPermission);
      } else if (statuses[currentPermission]!.isPermanentlyDenied) {
        onOpenSetting?.call(currentPermission);
      } else if (statuses[currentPermission]!.isRestricted) {
        //IOS单独处理
        onOpenSetting?.call(currentPermission);
      } else if (statuses[currentPermission]!.isLimited) {
        //IOS单独处理
        onOpenSetting?.call(currentPermission);
      }
    } else {
      onSuccess?.call();
    }

    return flag;
  }

  //申请权限
  static Future<PermissionStatus> requestPermission(List<Permission> permissionList) async {
    Map<Permission, PermissionStatus> statuses = await permissionList.request();
    PermissionStatus currentPermissionStatus = PermissionStatus.granted;
    statuses.forEach((key, value) {
      if (!value.isGranted) {
        currentPermissionStatus = value;
        return;
      }
    });
    return currentPermissionStatus;
  }
}
